# Replication Archive for: Schwarz, Susanne and Alexander Coppock. (2020) 
# "What Have We Learned About Gender From Candidate Choice Experiments? 
# A Meta-analysis of 67 Factorial Survey Experiments" 
# Forthcoming, Journal of Politics.

rm(list = ls())
library(tidyverse)
library(estimatr)
library(rmeta)
library(janitor)
library(reshape2)

source("helpers.R")


# Figure 3 ----------------------------------------------------------------

resp_gender_cate_estimates <-
  read_rds("resp_gender_cate_estimates.rds")

summary_df <-
  resp_gender_cate_estimates %>%
  group_by(resp_female) %>%
  do(tidy(with(
    ., meta.summaries(estimate, std.error, method = "random")
  )))

# is the difference-in-cates significant?
summary_df %>%
  melt(id.vars = c("resp_female")) %>%
  dcast(. ~ resp_female + variable) %>%
  clean_names() %>%
  transmute(
    estimate = female_respondents_estimate - male_respondents_estimate,
    std.error = sqrt(
      female_respondents_std_error ^ 2 + male_respondents_std_error ^ 2
    ),
    statistic = estimate / std.error,
    p.value = 2 * pnorm(abs(statistic), lower.tail = FALSE)
  )



summary_df <-
  summary_df %>%
  mutate(
    study_name_short = "Random-effects meta-analysis",
    estimate_100 = estimate * 100,
    std.error_100 = std.error * 100,
    conf.high_100 = conf.high * 100,
    conf.low_100 = conf.low * 100
  )

gg_df <-
  resp_gender_cate_estimates %>%
  bind_rows(summary_df) %>%
  mutate(
    facet = as.numeric(study_name_short == "Random-effects meta-analysis"),
    estimate_100 = estimate * 100,
    std.error_100 = std.error * 100,
    conf.high_100 = conf.high * 100,
    conf.low_100 = conf.low * 100,
    se_entry = make_se_entry(estimate_100, std.error_100, digits = 1)
  )

difference_df <-
  gg_df %>%
  select(study_name_short, resp_female, estimate) %>%
  melt(id.vars = c("study_name_short", "resp_female")) %>%
  dcast(study_name_short ~ resp_female + variable) %>%
  mutate(
    difference = `Female respondents_estimate` - `Male respondents_estimate`,
    study_name_short_fac = fct_reorder(study_name_short, difference)
  )

gg_df <-
  gg_df %>%
  mutate(study_name_short = factor(
    study_name_short,
    levels = levels(difference_df$study_name_short_fac)
  ))



# All estimates
g <-
  ggplot(gg_df, aes(x = estimate_100, y = study_name_short)) +
  geom_point(alpha = 0.8, stroke = 0) +
  geom_errorbarh(aes(xmin = conf.low_100, xmax = conf.high_100),
                 height = 0,
                 alpha = 0.5) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  geom_vline(data = summary_df, aes(xintercept = estimate_100)) +
  geom_rect(
    data = summary_df,
    aes(
      xmin = conf.low_100,
      xmax = conf.high_100,
      x = NULL,
      y = NULL
    ),
    ymin = 0,
    ymax = 100,
    alpha = .1
  ) +
  geom_text(
    x = 20,
    aes(y = study_name_short, label = se_entry),
    hjust = 1,
    size = 2,
    nudge_y = 0.2
  ) +
  coord_cartesian(xlim = c(-20, 20)) +
  xlab("CATE estimate in percentage points") +
  facet_grid(
    rows = vars(facet),
    cols = vars(resp_female),
    scales = "free_y",
    space = "free"
  ) +
  theme_bw() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_text(size = 8),
    legend.position = "bottom",
    strip.background = element_blank(),
    strip.text.y = element_blank()
  )

g

# ggsave(
#   "meta_cate_resp_gender.pdf",
#   g,
#   height = 5,
#   width = 6.5
# )
